//
//  Layout.swift
//  
//
//  Created by bo wang on 2020/10/21.
//  Copyright © 2020 wangbo. All rights reserved.
//

import Foundation
import UIKit

extension UIView {
    
    static func VFL(_ format: String, _ views: [String: UIView]? = nil, _ metrics: [String: Any]? = nil) -> [NSLayoutConstraint] {
        return NSLayoutConstraint.constraints(withVisualFormat: format, options: [], metrics: metrics, views: views ?? [:])
    }
    
    static func VFL(H hformat: String? = nil, V vformat: String? = nil, _ views: [String: UIView]? = nil, _ metrics: [String: Any]? = nil) -> [NSLayoutConstraint] {
        var constraints: [NSLayoutConstraint] = []
        if let h = hformat {
            constraints.append(contentsOf: VFL(h, views, metrics))
        }
        if let v = vformat {
            constraints.append(contentsOf: VFL(v, views, metrics))
        }
        return constraints
    }
    
    func VFL(H hformat: String? = nil, V vformat: String? = nil, _ views: [String: UIView]? = nil, _ metrics: [String: Any]? = nil) {
        views?.forEach({ $1.translatesAutoresizingMaskIntoConstraints = false })
        addConstraints(UIView.VFL(H: hformat, V: vformat, views, metrics))
    }
}

extension UILayoutPriority {
    static let almostRequired = UILayoutPriority(rawValue: 999)
    static let almostIgnore = UILayoutPriority(rawValue: 1)
}

let KEY_WINDOW = UIApplication.shared.keyWindow!

var SAFE_AREA_TOP: CGFloat {
    if #available(iOS 11.0, *) {
        return KEY_WINDOW.safeAreaInsets.top
    }
    return 0.0
}
var SAFE_AREA_BOTTOM: CGFloat {
    if #available(iOS 11.0, *) {
        return KEY_WINDOW.safeAreaInsets.bottom
    }
    return 0.0
}
var STATUS_BAR_HEIGHT: CGFloat {
    return UIApplication.shared.statusBarFrame.height
}
let NAVI_BAR_HEIGHT: CGFloat = 44
